$(function() {
	//返回顶部
	$(document).scroll(function() {
		if($(document).scrollTop() >= 100) {
			$('.backToTop').css('display', 'block');
		} else {
			$('.backToTop').css('display', 'none');
		}
	})
	$('.backToTop').click(function() {
		document.documentElement.scrollTop = document.body.scrollTop = 0;
	})
	//购物车内容
	if(!$.cookie('cart')) {
		$.ajax({
			type: "get",
			async: true,
			success: function(content) {
				var html = "";
				html =
					`<div class="cart_index_two"><div class="cart_none_nav"><div class="cart_none_left"><img src="http://image.xzhuang.com/static/index/img/u21.png"></div><div class="cart_none_right"><h4>您的购物车内暂无商品，赶快去挑选心爱的商品吧！</h4><a href="../index.html" class="return">回到首页</a><div class="pro"><p>你可以:</p><span>查看<a href="../order.html" target="_blank">我的订单</a><i>查看</i><a href="http://www.xzhuang.com/cas/user/favorite" target="_blank">我的收藏</a></span></div><div class="link">大家都在疯抢的热门品牌：<a href="/?brand-270.html" target="_blank">SKIN79</a><a href="http://www.xzhuang.com/?brand-732.html" target="_blank">SUNSHOP</a><a href="/?brand-525.html" target="_blank">韩束</a><a href="/?brand-222.html" target="_blank">菲诗小铺</a></div></div></div></div>`
				$('.cart_index').prepend(html);
			}
		});
	} else {
		$.ajax({
			type: "get",
			async: true,
			success: function(content) {
				//alert($.cookie('cart'))

				var cartObj = JSON.parse($.cookie('cart'));
				var count = 0;
				var total = 0;
				for(var i in cartObj) {
					count += cartObj[i].num;
					total += cartObj[i].num * parseFloat((cartObj[i].price).slice(1));
				}

				var htmlhead = `<div class="cart_list_tab">
					<table cellspadding="0" cellspacing="0" border="0">
						<tbody>
							<tr class="cartltab_title">
								<td width="386">商品名称</td>
								<td width="130">单价</td>
								<td width="80">使用积分</td>
								<td width="130">数量</td>
								<td width="130">赠送积分</td>
								<td width="130">小计</td>
								<td width="110">操作</td>
							</tr>
							
							<tr class="cartltab_count">
								<td colspan="7"><span class="cart_tub0 cart_tub2"></span>
									<a href="javascript:;" class="cleanProductFromCart">清空购物车</a>
									<script type="text/javascript">
										//清空购物车
										$('.cleanProductFromCart').click(function(){
											$.removeCookie('cart',{path:'/'});
											location.reload();
										})
									</script>
									<ul id="cartTotal">
										<li>商品数量总计：${count}件</li>
										<li>赠送积分总计：${total}分</li>
										<li>商品总额：</li>
										<li class="cart_prices">¥${total} </li>
										<input value="49.80" id="total_price_hidden" type="hidden">
									</ul>
								</td>
							</tr>
						</tbody>
					</table>

				</div>
				<div class="cart_youhui clearfix">

					<div class="cart_yhcount">
						<div id="cartPmt" class="clearfix">
							<h5>当前享受的优惠：</h5>
							<ol>
								<li>【换购】美迪惠尔A.P.L去角质黑头面膜(红色)15ml*2</li>
								<li>【换购】得鲜温和茶树面贴膜21ml*3</li>

							</ol>
						</div>
					</div>
				</div>
				<div class="cart_js">
					<span class="cart_tub0 cart_tub4"></span>
					<a class="cart_jx" href="../index.html">继续购物</a>
					<a href="javascript:;" class="cart_tub0 cart_tub5 checkoutCart" data-url="http://www.xzhuang.com/bts/order/finalPayment" data-login="1"><i style="font-weight: bold;">去结算</i></a>
				</div>`
				$('.cart_index').prepend(htmlhead);
				for(var id in cartObj) {
					var goodId = cartObj[id];
					var html = `<tr class="cartltabtr" id="tr-${id}">
                        <td align="left"><a href="" title="${goodId.name}" target="_blank" class="cartltab_pro"><img src="${goodId.src}" height="50" width="50"></a> <a href="" target="_top" class="cartltab_proname">${goodId.name}</a></td>
                        <td>${goodId.price}</td>
                        <td>0</td>
                        <td><span class="cartltab_num1 decreaseCartProductNum" data-product-id="${id}">-</span> <input class="cartltab_num updateCartProductNum" id="quantity-${id}" name="quantity" value="${goodId.num}" data-value="1" type="text" data-product-id="${id}"> <span class="cartltab_num2 increaseCartProductNum" data-product-id="${id}">+</span></td>
                        <td>${parseFloat((goodId.price).slice(1)) * goodId.num}</td>
                        <td class="tot">￥${parseFloat((goodId.price).slice(1)) * goodId.num}</td>
                        <td class="cart_caozuo"><a class="cct_list_num_2103 deleteGoodsFromCart" data-product-id="${id}" href="javascript:;">删除</a><br></td>
                    </tr>`
					$('.cartltab_title').after(html);

				}
				//删除
				//alert($('.cartltabtr').length)
				$('.deleteGoodsFromCart').click(function() {
					var id = $(this).attr('data-product-id');
					console.log(id)
					$(this).parents('tr').remove();
					var cartObj = JSON.parse($.cookie('cart'));
					delete cartObj[id];
					$.cookie('cart', JSON.stringify(cartObj), {
						expires: 7,
						path: "/"
					});

					if($('.cartltabtr').length == 0) {
						$.removeCookie('cart', {
							path: "/"
						});
					}
					location.reload();
				})

				//加
				$(".increaseCartProductNum").click(function() {
					//获取id
					var id = $(this).attr("data-product-id");
					//获取cookie
					var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
					var cartObj = JSON.parse(cartStr);
					cartObj[id].num += 1;
					$(this).prev(".updateCartProductNum").val(cartObj[id].num);
					//小计
//					$(this).parent().siblings(".tot").prev('td').html(cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
//					$(this).parent().siblings(".tot").html("¥" + cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
					//重新加入cookie
					$.cookie("cart", JSON.stringify(cartObj), {
						expires: 7,
						path: "/"
					})
					location.reload();
				})
				//-
				$(".decreaseCartProductNum").click(function() {
					//获取id
					var id = $(this).attr("data-product-id");
					//获取cookie
					var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
					var cartObj = JSON.parse(cartStr);
					if(cartObj[id].num > 1) {
						cartObj[id].num -= 1;
						$(this).next(".updateCartProductNum").val(cartObj[id].num);
						//小计
//						$(this).parent().siblings(".tot").prev('td').html(cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
//						$(this).parent().siblings(".tot").html("¥" + cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
						//重新加入cookie
						$.cookie("cart", JSON.stringify(cartObj), {
							expires: 7,
							path: "/"
						})
						location.reload();
					}
					

				})
				//给文本框加失焦事件
				$(".updateCartProductNum").blur(function(){
					var id = $(this).attr("data-product-id");
					var cartObj = JSON.parse( $.cookie("cart"));
					var re = /^\d+$/;
					if(re.test($(this).val()) && parseInt($(this).val()) > 0){
						cartObj[id].num = parseInt($(this).val());
					}else{
						cartObj[id].num = 1;
						$(this).val(1);
					}
//					$(this).parent().siblings(".tot").prev('td').html(cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
//					$(this).parent().siblings(".tot").html("¥" + cartObj[id].num * parseFloat(cartObj[id].price.slice(1)));
					$.cookie("cart",JSON.stringify(cartObj),{expires : 7,path : "/"});
					location.reload();
				})
				//猜你喜欢
				$('.cart_tub6').click(function(){
					alert('没有库存了');
				})
			}
		})
	}
})